package com.user.action

import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.Account
import com.data.bean.Theme
import com.data.util.FinalStringUtils
import com.data.util.ImageLookUtils
import com.data.util.JsonUtils
import com.user.service.ThemeAdminService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.commons.CommonsMultipartFile

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * FileName: ThemeAdminAction
 * Author:   HingLo
 * Date:     2017/11/6 11:40
 * Description: 主题管理
 */
@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("themeAdmin/user/")
class ThemeAdminAction {
    @Autowired
    private val themeAdminService: ThemeAdminService? = null

    /**
     * 主题列表
     */
    @GetMapping("list")
    fun listThemeAdminAction(request: HttpServletRequest): String {
        val account = request.session.getAttribute(FinalStringUtils.ACCOUNT) as Account
        val list = themeAdminService!!.listThemeAdminService(account.id)
        request.setAttribute("dataMap", list)
        return "WEB-INF/user/theme/listTheme"
    }

    /**
     * 查看图片
     *
     * @param image    图片的名称
     * @param response
     */
    @GetMapping("image")
    fun showImageAction(@RequestParam("image")image: String,@RequestParam("tid") tid: String, response: HttpServletResponse) {
        val inputStream = themeAdminService!!.showImageService(image, tid)
        //响应图片
        if (inputStream != null) {
            ImageLookUtils.responseImage(inputStream, response)
        }

    }

    /**
     * 进入到添加主题的界面
     */
    @GetMapping("addTheme")
    fun addThemeAdminAction(request: HttpServletRequest): String = "WEB-INF/user/theme/addTheme"


    /**
     * 添加主题
     *
     * @param images
     * @param bImages
     * @param theme
     * @param request
     * @return
     */
    @PostMapping("submitThemeAdmin")
    @ResponseBody
    fun submitThemeAdminAction(@RequestParam("images") images: CommonsMultipartFile, @RequestParam("bImages") bImages: CommonsMultipartFile, theme: Theme, request: HttpServletRequest): Map<String, Any> {

        val account = request.session.getAttribute(FinalStringUtils.ACCOUNT) as Account
        theme.uid = account.id

        val bool = themeAdminService!!.submitThemeAdminService(images, bImages, theme)
        return JsonUtils.returnMassageUtils(bool, if (bool) "添加成功" else "添加失败")
    }

    /**
     * 提交审核
     *
     * @param id
     * @return
     */

    @PostMapping("changeTheme")
    @ResponseBody
    fun changeThemeAdminAction(id: String, state: Int): Map<String, Any> {
        val bool = themeAdminService!!.changeThemeAdminService(id, state)
        return JsonUtils.returnMassageUtils(bool, if (bool) "提交成功" else "提交失败")
    }

    /**
     * 修改顺序
     *
     * @param id
     * @return
     */

    @PostMapping("softTheme")
    @ResponseBody
    fun changeThemeSoftAction(@RequestParam("id")id: String): Map<String, Any> {
        val bool = themeAdminService!!.changeThemeSoftService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "提交成功" else "提交失败")
    }


    /**
     * 删除未上线的主题
     *
     * @param id
     * @return
     */

    @PostMapping("delete")
    @ResponseBody
    fun deleteThemeAdminAction(@RequestParam("id")id: String): Map<String, Any> {
        val bool = themeAdminService!!.deleteThemeAdminService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }

    /**
     * 修改没有上线的主题信息
     *
     * @param id
     * @return
     */

    @PostMapping("changeMassage")
    @ResponseBody
    fun changeMassageThemeAdminAction(@RequestParam("id") id: String, @RequestParam("type") type: Int, @RequestParam("value") value: String): Map<String, Any> {
        val bool = themeAdminService!!.changeMassageThemeAdminService(id, type, value)
        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }

    /**
     * 修改没有上线的主题信息的图片
     *
     * @param id
     * @return
     */

    @PostMapping("changeMassageImage")
    @ResponseBody
    fun changeMassageImageThemeAdminAction(@RequestParam("id") id: String, @RequestParam("type") type: Int, @RequestParam("value") value: CommonsMultipartFile): Map<String, Any> {
        println(type)
        println(id)
        val bool = themeAdminService!!.changeMassageImageThemeAdminService(id, type, value)
        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }

    /**
     * 审核没有通过的提示信息
     *
     * @param id
     * @return
     */

    @PostMapping("tipError")
    @ResponseBody
    fun tipErrorAction(@RequestParam("id") id: String): Map<String, Any> {
        val bool: String = themeAdminService!!.tipErrorService(id)
        return JsonUtils.returnMassageUtils(true, if (bool.isNotEmpty()) bool else "请联系管理员咨询原因")
    }


}
